package tech.lq0.dreamaticvoyage.item.fukamizutech;

import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.util.Mth;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class UCEnergyBlockItem extends BlockItem {

    private final int defaultMaxEnergy;

    public UCEnergyBlockItem(Block pBlock, Properties pProperties, int defaultMaxEnergy) {
        super(pBlock, pProperties.stacksTo(1));
        this.defaultMaxEnergy = defaultMaxEnergy;
    }

    @Override
    public boolean isBarVisible(ItemStack pStack) {
        return BlockItem.getBlockEntityData(pStack) != null;
    }

    @Override
    public int getBarColor(ItemStack pStack) {
        return 0xB692C4;
    }

    @Override
    public int getBarWidth(ItemStack pStack) {
        CompoundTag tag = BlockItem.getBlockEntityData(pStack);

        int energy = tag == null ? 0 : tag.getInt("UmisuEnergy");
        int maxEnergy = this.defaultMaxEnergy;
        if (maxEnergy == 0) {
            return 0;
        }

        return Math.round(13.0F - (float) (maxEnergy - energy) * 13.0F / (float) maxEnergy);
    }

    @Override
    public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltip, TooltipFlag pFlag) {
        super.appendHoverText(pStack, pLevel, pTooltip, pFlag);

        CompoundTag tag = BlockItem.getBlockEntityData(pStack);

        int energy = tag == null ? 0 : tag.getInt("UmisuEnergy");
        int maxEnergy = this.defaultMaxEnergy;
        float percentage = Mth.clamp((float) energy / maxEnergy, 0, 1);
        MutableComponent component = Component.literal("");

        int count = (int) (percentage * 50);
        for (int i = 0; i < count; i++) {
            component.append(Component.literal("|").withStyle(Style.EMPTY.withColor(0xB692C4)));
        }
        component.append(Component.literal("").withStyle(ChatFormatting.RESET));
        for (int i = 0; i < 50 - count; i++) {
            component.append(Component.literal("|").withStyle(ChatFormatting.GRAY));
        }

        component.append(Component.literal(" " + energy + "/" + maxEnergy + " UCE").withStyle(ChatFormatting.GRAY));
        pTooltip.add(component);
    }

    public static ItemStack makeFullEnergyStack(ItemLike itemLike, BlockEntityType<?> blockEntityType, int defaultMaxEnergy) {
        ItemStack stack = new ItemStack(itemLike);
        CompoundTag tag = new CompoundTag();
        tag.putInt("UmisuEnergy", defaultMaxEnergy);
        tag.putInt("MaxUmisuEnergy", defaultMaxEnergy);
        BlockItem.setBlockEntityData(stack, blockEntityType, tag);
        return stack;
    }
}
